<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="5" :lg="7" :md="8" :sm="24">
            <a-form-item label="任务描述">
              <j-input placeholder="请输入任务描述" v-model="queryParam.description"></j-input>
            </a-form-item>
          </a-col>
          <a-col :xl="12" :lg="11" :md="12" :sm="24">
            <a-form-item label="被执行的时间点">
              <j-date :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择开始时间"
                      class="query-group-cust" v-model="queryParam.overTime_begin"></j-date>
              <span class="query-group-split-cust"></span>
              <j-date :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择结束时间"
                      class="query-group-cust" v-model="queryParam.overTime_end"></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-wrap"
        @change="handleTableChange">

        <template slot="isOverSlot" slot-scope="text">
          <a-tag color="red" v-if="text===0">
            未执行
          </a-tag>
          <a-tag color="green" v-else>
            已执行
          </a-tag>
        </template>

        <template slot="isErrorSlot" slot-scope="text,record">
          <a-tag color="green" v-if="text===0">
            正常
          </a-tag>
          <a-tag color="red" v-else>
            <a @click="showErrorInfo(record.exceptionInfo)">异常</a>
          </a-tag>
        </template>

        <span slot="action" slot-scope="text, record">
          <b-button v-if="record.isOver===0 && record.isError===0" :toolTip="'立即执行'"
                    :type="'primary'"
                    :icon="'caret-right'"
                    @click="executeNow(record.id)"/>
        </span>
      </a-table>
    </div>
    <a-modal title="异常内容" width="80%" v-model="errorModal">
      <div v-html="errorInfo"></div>
    </a-modal>
  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import {mixinDevice} from '@/utils/mixin'
import {JeecgListMixin} from '@/mixins/JeecgListMixin'
import {putAction} from "@/api/manage";
import {BoneCommonMixin} from '@/mixins/BoneCommonMixin'
import JDate from '@/components/jeecg/JDate.vue'
import JSwitch from '@/components/jeecg/JSwitch'
import BButton from "@/components/bone/BButton";
import JInput from "@/components/jeecg/JInput"

export default {
  name: 'CmsDelayTaskList',
  mixins: [JeecgListMixin, mixinDevice, BoneCommonMixin],
  components: {
    JDate,
    JSwitch,
    BButton,
    JInput,
  },
  data() {
    return {
      description: '延时处理管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 30,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          }
        },
        {
          title: '加入时间',
          align: "center",
          dataIndex: 'createTime'
        },
        {
          title: '执行状态',
          align: "center",
          dataIndex: 'isOver',
          scopedSlots: {customRender: "isOverSlot"}
        },
        {
          title: '任务描述',
          align: "center",
          dataIndex: 'description',
        },
        {
          title: '应该执行的时间',
          align: "center",
          dataIndex: 'overTime'
        },
        {
          title: '异常状态',
          align: "center",
          dataIndex: 'isError',
          scopedSlots: {customRender: "isErrorSlot"}
        },
        {
          title: '被操作的时间点',
          align: "center",
          dataIndex: 'updateTime'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          fixed: "right",
          width: 147,
          scopedSlots: {customRender: 'action'}
        }
      ],
      url: {
        list: "/delay/cmsDelayTask/list",
        execute: "/delay/cmsDelayTask/executeLocalTask",
      },
      isorter: {
        column: 'createTime',
        order: 'desc',
      },
      dictOptions: {},
      errorModal: false,
      errorInfo: null,
    }
  },
  methods: {
    executeNow: function (id) {
      this.loading = true;
      putAction(this.url.execute + "/" + id).then(res => {
        if (res.code === 200) {
          this.showSuccess(res.message);
          this.loadData();
        } else {
          this.showError(res.message);
        }
      }).finally(() => {
        this.loading = false;
      });
    },
    showErrorInfo: function (text) {
      this.errorModal = true;
      this.errorInfo = text;
    }
  }
}
</script>
<style scoped>
</style>